package com.seagox.chat.util;

import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.stereotype.Component;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
 
@Component
public class DocumentConverterUtils {
 
	@SuppressWarnings("deprecation")
	public void convert(InputStream inputStream, OutputStream outputStream) {
		try {
			RandomAccessFileOrArray randomAccessFileOrArray = new RandomAccessFileOrArray(inputStream);
			// 读取tiff文件页数
			int numberOfPages = TiffImage.getNumberOfPages(randomAccessFileOrArray);
			Document document = new Document();
			PdfWriter.getInstance(document, outputStream);
			document.open();
			// 读取tiff文件的图像信息，添加到pdf文件
			for (int i = 1; i <= numberOfPages; i++) {
				Image tempImage = TiffImage.getTiffImage(randomAccessFileOrArray, i);
				Rectangle pageSize = new Rectangle(tempImage.getWidth(), tempImage.getHeight());
				document.setPageSize(pageSize);
				document.newPage();
				document.add(tempImage);
			}
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
}
